﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace shop.Areas.admin.Controllers
{
    public class MauController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /admin/Mau/

        public ActionResult Index()
        {
            return View(db.Maus.ToList());
        }

        //
        // GET: /admin/Mau/Details/5

        public ActionResult Details(int id = 0)
        {
            Mau mau = db.Maus.Find(id);
            if (mau == null)
            {
                return HttpNotFound();
            }
            return View(mau);
        }

        //
        // GET: /admin/Mau/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /admin/Mau/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Mau mau, HttpPostedFileBase AnhMau)
        {
            var path = Server.MapPath("/images/mau/");
            string da = DateTime.Now.ToString("yyyyddmmhhMMss");
            if(AnhMau !=null )
            {
                mau.HinhAnh = Path.GetFileName(da+AnhMau.FileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                AnhMau.SaveAs(path+mau.HinhAnh);
            }
            else
            {
                mau.HinhAnh = "noimages.jpg";
            }

            if (ModelState.IsValid)
            {
                db.Maus.Add(mau);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(mau);
        }

        //
        // GET: /admin/Mau/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Mau mau = db.Maus.Find(id);
            if (mau == null)
            {
                return HttpNotFound();
            }
            return View(mau);
        }

        //
        // POST: /admin/Mau/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Mau mau,string anhcu,HttpPostedFileBase AnhMau)
        {
            var path = Server.MapPath("/images/mau/");
            string da = DateTime.Now.ToString("yyyyddmmhhMMss");
           if(AnhMau !=null )
           {
               mau.HinhAnh = Path.GetFileName(AnhMau.FileName);
               if (!Directory.Exists(path))
                   Directory.CreateDirectory(path);
               AnhMau.SaveAs(path+mau.HinhAnh);
           }
           else
           {
               mau.HinhAnh = anhcu;
           }
            if (ModelState.IsValid)
            {
                db.Entry(mau).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(mau);
        }

        //
        // GET: /admin/Mau/Delete/5

        //public ActionResult Delete(int id = 0)
        //{
        //    Mau mau = db.Maus.Find(id);
        //    if (mau == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(mau);
        //}

        //
        // POST: /admin/Mau/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(int id)
        {
            Mau mau = db.Maus.Find(id);
            try
            {
                db.Maus.Remove(mau);
                db.SaveChanges();
                return Content(Boolean.TrueString);
            }
            catch
            {
                return Content(Boolean.FalseString);
            }
           
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}